import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"

enum Api {
  page = "/infra-service/codeTemplate/pageTemplate",
  listVelocityVar = "/infra-service/codeTemplate/listVelocityVar",
  listByGroupId = "/infra-service/codeTemplate/listTemplateByGroupId",
  copy = "/infra-service/codeTemplate/copyTemplate",
  save = "/infra-service/codeTemplate/saveTemplate",
  edit = "/infra-service/codeTemplate/updateTemplate",
  get = "/infra-service/codeTemplate/getTemplate",
  delete = "/infra-service/codeTemplate/deleteTemplate",
  deleteBatch = "/infra-service/codeTemplate/batchDeleteTemplate",
  importExcel = "/infra-service/codeTemplate/importExcel",
  exportXls = "/infra-service/codeTemplate/exportXls",
}

/**
 * 导出api
 */
export const getExportUrl = Api.exportXls

export const getImportUrl = Api.importExcel

/**
 * 查询模板列表
 * @param params
 */
export const pageTemplate = (params) => {
  return defHttp.get({ url: Api.page, params })
}

/**
 * 查询模板列表
 * @param params
 */
export const listByGroupId = (params) => {
  return defHttp.get({ url: Api.listByGroupId, params })
}

/**
 * 查询模板引擎Velocity变量列表
 * @param params
 */
export const listVelocityVar = () => {
  return defHttp.get({ url: Api.listVelocityVar })
}

/**
 * 保存或者更新模板
 * @param params
 */
export const saveOrUpdateTemplate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save
  return defHttp.post({ url: url, params })
}

/**
 * 拷贝模板
 * @param params
 */
export const copyTemplate = (params) => {
  return defHttp.post({ url: Api.copy, params })
}

/**
 * 查询模板详情
 * @param params
 */
export const getTemplateById = (params) => {
  return defHttp.get({ url: Api.get, params })
}

/**
 * 删除模板
 * @param params
 */
export const deleteTemplate = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

/**
 * 批量删除模板
 * @param params
 */
export const batchDeleteTemplate = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}
